using System;
using System.Linq;
using System.ServiceModel.Description;
namespace Net35.Xrm.Sdk.Client
{
	public static class AuthenticationHelpers
	{
		internal static bool ShouldAuthenticateWithLiveId<TService>(this IServiceManagement<TService> serviceManagement, System.ServiceModel.Description.ClientCredentials clientCredentials)
		{
			OnlinePolicyConfiguration policyConfiguration = serviceManagement.PolicyConfiguration as OnlinePolicyConfiguration;
			return AuthenticationHelpers.ShouldAuthenticateWithLiveIdInternal(policyConfiguration, serviceManagement.AuthenticationType, clientCredentials);
		}
		internal static bool ShouldAuthenticateWithLiveId<TService>(this IServiceConfiguration<TService> serviceConfiguration, System.ServiceModel.Description.ClientCredentials clientCredentials)
		{
			OnlinePolicyConfiguration policyConfiguration = serviceConfiguration.PolicyConfiguration as OnlinePolicyConfiguration;
			return AuthenticationHelpers.ShouldAuthenticateWithLiveIdInternal(policyConfiguration, serviceConfiguration.AuthenticationType, clientCredentials);
		}
		internal static bool ShouldAuthenticateWithLiveId<TService>(this ServiceConfiguration<TService> serviceConfiguration, System.ServiceModel.Description.ClientCredentials clientCredentials)
		{
			OnlinePolicyConfiguration policyConfiguration = serviceConfiguration.PolicyConfiguration as OnlinePolicyConfiguration;
			return AuthenticationHelpers.ShouldAuthenticateWithLiveIdInternal(policyConfiguration, serviceConfiguration.AuthenticationType, clientCredentials);
		}
		private static bool ShouldAuthenticateWithLiveIdInternal(OnlinePolicyConfiguration policyConfiguration, AuthenticationProviderType authenticationProviderType, System.ServiceModel.Description.ClientCredentials clientCredentials)
		{
			switch (authenticationProviderType)
			{
			case AuthenticationProviderType.LiveId:
				return true;
			case AuthenticationProviderType.OnlineFederation:
				if (policyConfiguration != null && !StringExtensions.IsNullOrWhiteSpace(clientCredentials.UserName.UserName))
				{
					OrgIdentityProviderTrustConfiguration orgIdentityProviderTrustConfiguration = policyConfiguration.OnlineProviders.Values.OfType<OrgIdentityProviderTrustConfiguration>().FirstOrDefault<OrgIdentityProviderTrustConfiguration>();
					if (orgIdentityProviderTrustConfiguration == null)
					{
						return false;
					}
					IdentityProvider identityProvider = IdentityProviderLookup.Instance.GetIdentityProvider(orgIdentityProviderTrustConfiguration.Endpoint.GetServiceRoot(), orgIdentityProviderTrustConfiguration.Endpoint.GetServiceRoot(), clientCredentials.UserName.UserName);
					if (identityProvider != null && identityProvider.IdentityProviderType == IdentityProviderType.LiveId)
					{
						OnlineFederationPolicyConfiguration onlineFederationPolicyConfiguration = policyConfiguration as OnlineFederationPolicyConfiguration;
						onlineFederationPolicyConfiguration.InitializeLiveTrustConfiguration(identityProvider);
						return true;
					}
				}
				return false;
			default:
				return false;
			}
		}
	}
}
